<?php
/**
 * Created by PhpStorm.
 * Script Name: HandlerGroupMemberDecrease.php
 * Create: 2022/7/12 15:03
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\bot\handler;

use app\common\service\Tenant as TenantService;
use plugin\wechat\app\service\BotFriend as FriendService;
use plugin\wechat\app\service\BotGroupmember as GroupMemberService;

class HandlerGroupMemberDecrease extends Handler
{
    public function handle(){
        $this->group = FriendService::getMember($this->groupWxid, $this->botWxid);
        $this->basic();
        $this->addon();
    }

    protected function basic(){
        $request = \request();
        GroupMemberService::rmMember([
            'company_id' => $request->bot['company_id'],
            'bot_id' => $request->bot['id'],
            'wxid' => $request->content['to_wxid'],
            'group_id' => $request->group['id']
        ]);
    }

    protected function addon()
    {
        $apps = TenantService::getCompanyActiveApps(TenantService::getCompanyById($this->bot['company_id']), [['name','<>', 'wechat']]);
        foreach ($apps as $k => $v){
            $class_name = '\\plugin\\'.$v['name'].'\\app\\platform\\controller\\Bot';
            if(class_exists($class_name)){
                $class = new $class_name();
                if(method_exists($class, 'groupMemberDecreaseHandle')){
                    $class->groupMemberDecreaseHandle();
                }
            }
        }
    }
}